//
//  ScrollControllerView.swift
//  CViews
//
//  Created by xhl_dabaoMac on 2021/11/16.
//

import UIKit

 public class ScrollControllerView: UIView {
    
    var controllers : [UIViewController]?
    var selectIndexAction : ((Int) -> Void)?

    
    let collectionView : UICollectionView = {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = .horizontal
        let collection = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collection.isPagingEnabled = true
        collection.bounces = false
        return collection
        
    }()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        collectionView.delegate = self
        collectionView.dataSource = self
        addSubview(collectionView)
        collectionView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: "cellIdentifier")
        
        
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

extension ScrollControllerView : UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout {
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        controllers?.count ?? 0
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cellIdentifier", for: indexPath)
    
        guard let cons = controllers else { return cell }
        let con = cons[indexPath.row]
        con.view.removeFromSuperview()
        cell.addSubview(con.view)
        con.view.snp.makeConstraints { make in
            make.top.left.right.bottom.equalToSuperview()
        }
        return cell
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        CGSize(width:collectionView.bounds.width - 0.001 , height: collectionView.bounds.height - 0.001)
    }
}


extension ScrollControllerView {
    
    /// 滑动结局后根据偏移量，计算当前的索引
    /// - Parameter scrollView: collectionView
    public func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        guard let act = selectIndexAction else { return }
        ///向上取整，返回item的时候小了0.001
        let offsetx = ceil(scrollView.contentOffset.x)
        let selectIndex = Int(offsetx) / Int(scrollView.frame.size.width)
        act(selectIndex)
    }
    
    /// 外面设置collection View的偏移量
    /// - Parameters:
    ///   - index: 偏移多个屏幕的宽度
    ///   - animated: 是否做动画
    func setSelectIndex(index:Int,animated : Bool = true)  {
        collectionView.setContentOffset(CGPoint(x: Int(collectionView.frame.size.width)*index, y: 0), animated: animated)
    }
}
